var currHeight;
var currWidth;
var currTop;
var currLeft;
var minimizou = false;
var firstTime = false;

function initLoader() {
	resizeApp();
	loadMaps();
}

function resizeApp() {

	document.getElementById('content').style.height = (currentHeight() - 50) + 'px';
	document.getElementById('contentMap').style.height = (currentHeight() - 50) + 'px';
	document.getElementById('contentMonitoramento').style.height = (currentHeight() - 50) + 'px';
	document.getElementById('monitoramentoTable').style.height = (currentHeight() - 95) + 'px';

	try {
		var nameSize = document.getElementById('userLogin').firstChild.nodeValue.length;
		if(document.getElementById('userLogin')){
			document.getElementById('userLogin').style.paddingLeft = (currentWidth() - (840 + nameSize)) + 'px';
		}

		if( document.getElementById('relatorioTable') ){
			document.getElementById('relatorioTable').style.maxHeight = (currentHeight() - 230) + 'px';
			document.getElementById('relatorioTable').height = (currentHeight() - 230) + 'px';
		}

		if( document.getElementById('innerPopup_body') ){
			document.getElementById('innerPopup_body').height = (currentHeight() - 230) + 'px';
		}

		if( document.getElementById('innerPopup') ){
			document.getElementById('innerPopup').height = (currentHeight()) + 'px';
		}

	} catch (e) {}

	window.onresize = resizeApp;
	openCloseList(true);
}

function currentWidth() {

    var width = 0;
    if (typeof(window.innerWidth) == 'number') {
    	width = window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
    	width = document.documentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
    	width = document.body.clientWidth;
    }
    return width;
}

function currentHeight() {

    var height = 0;
    if (typeof(window.innerHeight) == 'number') {
    	height = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
    	height = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
    	height = document.body.clientHeight;
    }
    return height;
}

function openCloseList(reSize) {

	if (reSize) {
		document.getElementById('contentMap').style.width = currentWidth() + 'px';
		if(document.getElementById('maparea')){
			document.getElementById('maparea').style.height = (currentHeight() - 220) + 'px';
		}

		document.getElementById('contentMonitoramento').style.display = 'none';
		document.getElementById('imgOpen').style.display = '';
		document.getElementById('imgClose').style.display = 'none';
		if(document.getElementById('innerPopup')){

			if(!minimizou){

				document.getElementById('innerPopup').style.width = (currentWidth() - 70) + 'px';
				document.getElementById('innerPopup_body').style.width = (currentWidth() - 90) + 'px';

				if (!currHeight) {
					currHeight = document.getElementById('innerPopup').offsetHeight;
				}

				if (!currTop) {
					currTop = document.getElementById('innerPopup').offsetTop;
				}

				if (!currLeft) {
					currLeft = document.getElementById('innerPopup').offsetLeft;
				}

			} else {
				document.getElementById('innerPopup').style.left = (currentWidth() - 150) + 'px';
			}
		}

	} else {

		if (document.getElementById('contentMonitoramento').style.display == '') {
			document.getElementById('contentMap').style.width = currentWidth() + 'px';
			document.getElementById('contentMonitoramento').style.display = 'none';
			document.getElementById('imgOpen').style.display = '';
			document.getElementById('imgClose').style.display = 'none';

		} else {
			document.getElementById('contentMap').style.width = (currentWidth() - 500) + 'px';
			document.getElementById('contentMonitoramento').style.display = '';
			document.getElementById('imgOpen').style.display = 'none';
			document.getElementById('imgClose').style.display = '';
		}
	}

	try {
		map.checkResize();
		calculateBounds();
	} catch(err) {}
}

function minimizePopup(){

	if( minimizou == false){
		if(firstTime == false){
			currTop = document.getElementById('innerPopup').offsetTop;
			currLeft = document.getElementById('innerPopup').offsetLeft;
			currHeight = document.getElementById('innerPopup').offsetHeight;
			currWidth = document.getElementById('innerPopup').offsetWidth;
			firstTime = true;
		}
		document.getElementById('innerPopup').style.width = 120 + 'px';
		document.getElementById('innerPopup').style.height = 16 + 'px';
		document.getElementById('innerPopup').style.left = (currentWidth() - 150) + 'px';
		document.getElementById('innerPopup').style.top = (currentHeight() - 40) + 'px';
		var elements = document.getElementById('innerPopup').childNodes;

		for ( var j = 0; j < elements.length; j++) {
			var e = elements[j];
			var innerp = 'innerPopup_header';
			if(!(e.id === innerp)){
				elements[j].style.display = 'none';
			}
		}
		minimizou = true;
	}
}

function maximizePopup(){

	if( minimizou == true){
		document.getElementById('innerPopup').style.width = (currentWidth() - 70) + 'px';
		if (!currHeight) {
			currHeight = document.getElementById('innerPopup').offsetHeight;
		}
		document.getElementById('innerPopup').style.height = currHeight + 'px';
		document.getElementById('innerPopup_body').style.display = 'block';

		if (!currTop) {
			currTop = document.getElementById('innerPopup').offsetTop;
		}

		document.getElementById('innerPopup').style.top = currTop + 'px';

		if (!currLeft) {
			currLeft = document.getElementById('innerPopup').offsetLeft;
		}

		document.getElementById('innerPopup').style.left = (currLeft) + 'px';

		minimizou = false;
	}
}

function habilitarPoll(){

	var vpoll = document.getElementById('pollAction');
	alert(vpoll);
	if( vpoll.value == true){
		vpoll.value = false;
	} else if(vpoll.value == false){
		vpoll.value = true;
	}
}

function showAddress() {
	mapp.clearOverlays();
	var address = document.getElementById('consultaPonto').value;
	if (geocoder) {
	  geocoder.getLatLng(
      address,
      function(point) {
    	  mapp.setCenter(point, 15);
          var marker = new GMarker(point);
          mapp.addOverlay(marker);
          document.getElementById("lat").value = point.lat();
          document.getElementById("lng").value = point.lng();
          document.getElementById("nomePonto").value = address;
      }
    );
  }
}

